package com.purong.purong.activity;

import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.TimePickerView;
import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.bean.UserCompanyDataBean;
import com.purong.purong.bean.UserCompanyBean;
import com.purong.purong.bean.TextItemBean;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.util.NumDecTextWatch;
import com.purong.purong.widget.LoadingView;
import com.purong.purong.widget.TextChoseDialog;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zengwenjie on 2017/12/12.
 */
public class UserCompanyActivity extends BaseActivity {
    private TextView regTimeTv,typeTv,logTv;
    private EditText companyEd,lawEd,managerEd,phoneEd,capitalEd,ratioEd,flowInEd,flowOutEd,licenseAddEd,officeAddEd;
    private TextChoseDialog mTypeDialog,mLogDialog;
    private TextItemBean typeBean,logBean;
    private LoadingView loadingView;
    private UserCompanyBean bean;
    @Override
    public int getRootViewId() {
        return R.layout.activity_user_company;
    }

    @Override
    public void initView() {

        showBack();
        showTitle("企业基本信息");
        loadingView=(LoadingView)findViewById(R.id.loading_view);
        loadingView.setLoadListener(new LoadingView.OnLoadListener() {
            @Override
            public void onReload() {
                getCompanyInfo();
            }

            @Override
            public void onRefresh() {

            }
        });
        regTimeTv=(TextView)findViewById(R.id.reg_time_tv);
        typeTv=(TextView)findViewById(R.id.type_tv);
        logTv=(TextView)findViewById(R.id.log_tv);
        companyEd=(EditText)findViewById(R.id.company_ed);
        lawEd=(EditText)findViewById(R.id.law_ed);
        managerEd=(EditText)findViewById(R.id.manager_ed);
        phoneEd=(EditText)findViewById(R.id.phone_ed);
        capitalEd=(EditText)findViewById(R.id.capital_ed);
        ratioEd=(EditText)findViewById(R.id.ratio_ed);
        flowInEd=(EditText)findViewById(R.id.flow_in_ed);
        flowOutEd=(EditText)findViewById(R.id.flow_out_ed);
        licenseAddEd=(EditText)findViewById(R.id.license_add_ed);
        officeAddEd=(EditText)findViewById(R.id.office_add_ed);
        capitalEd.addTextChangedListener(new NumDecTextWatch(capitalEd));
        ratioEd.addTextChangedListener(new NumDecTextWatch(ratioEd));
        flowOutEd.addTextChangedListener(new NumDecTextWatch(flowOutEd));
        flowInEd.addTextChangedListener(new NumDecTextWatch(flowInEd));

    }
    private void getCompanyInfo(){
        loadingView.loading();
        Map<String, String> params = new LinkedHashMap<String, String>();
        if (!request(Contant.COMPANY_INFO, params, true,false)){
            loadingView.fail();
        }
    }
    private void setCompanyInfo(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        if (companyEd.getText().toString().trim().equals("")){
            showToast("企业名称为必填项");
            return;
        }else {
            params.put("company_name",companyEd.getText().toString().trim());
        }
        if (lawEd.getText().toString().trim().equals("")){
            showToast("企业法人为必填项");
            return;
        }else {
            params.put("law_man",lawEd.getText().toString().trim());
        }
        params.put("manager",managerEd.getText().toString().trim());
        if (phoneEd.getText().toString().trim().equals("")){
            showToast("联系电话为必填项");
            return;
        }else {
            params.put("phone",phoneEd.getText().toString().trim());
        }
        if (capitalEd.getText().toString().trim().equals("")){
            showToast("注册资本为必填项");
            return;
        }else {
            params.put("register_capital",capitalEd.getText().toString().trim());
        }
        if (regTimeTv.getText().equals("请选择")){
            showToast("注册日期为必选项");
            return;
        }else {
            params.put("register_time",regTimeTv.getText().toString().trim());
        }
        if (typeBean!=null){
            params.put("business_type",typeBean.getId());
        }
        params.put("hold_ratio",ratioEd.getText().toString().trim());
        params.put("flow_out",flowOutEd.getText().toString().trim());
        params.put("flow_in",flowInEd.getText().toString().trim());
        if (logBean!=null){
            params.put("court_log",logBean.getId());
        }
        params.put("license_address",licenseAddEd.getText().toString().trim());
        params.put("office_address",officeAddEd.getText().toString().trim());
        request(Contant.COMPANY_EDIT, params, true,true);
    }
    private void setData(){
        if (bean==null)
            return;
        if (bean.getCompany_name()!=null){
            companyEd.setText(bean.getCompany_name());
        }
        if (bean.getLaw_man()!=null){
            lawEd.setText(bean.getLaw_man());
        }
        if (bean.getManager()!=null){
            managerEd.setText(bean.getManager());
        }
        if (bean.getPhone()!=null){
            phoneEd.setText(bean.getPhone());
        }
        if (bean.getRegister_capital()!=null){
            capitalEd.setText(bean.getRegister_capital());
        }
        if (bean.getRegister_time()!=null&&!bean.getRegister_time().equals("")){
            regTimeTv.setText(bean.getRegister_time());
            regTimeTv.setTextColor(getResources().getColor(R.color.blue));
        }
        if (bean.getCompany_type()!=null){
            switch (bean.getCompany_type()){
                case "1":
                    typeTv.setText("未知");
                    typeTv.setTextColor(getResources().getColor(R.color.blue));
                    typeBean=new TextItemBean();
                    typeBean.setId("1");
                    typeBean.setName("未知");
                    break;
                case "2":
                    typeTv.setText("国企");
                    typeTv.setTextColor(getResources().getColor(R.color.blue));
                    typeBean=new TextItemBean();
                    typeBean.setId("2");
                    typeBean.setName("国企");
                    break;
                case "3":
                    typeTv.setText("私企");
                    typeTv.setTextColor(getResources().getColor(R.color.blue));
                    typeBean=new TextItemBean();
                    typeBean.setId("3");
                    typeBean.setName("私企");
                    break;
                case "4":
                    typeTv.setText("个体");
                    typeTv.setTextColor(getResources().getColor(R.color.blue));
                    typeBean=new TextItemBean();
                    typeBean.setId("4");
                    typeBean.setName("个体");
                    break;
            }
        }
        if (bean.getHold_ratio()!=null){
            ratioEd.setText(bean.getHold_ratio());
        }
        if (bean.getFlow_in()!=null){
            flowInEd.setText(bean.getFlow_in());
        }
        if (bean.getFlow_out()!=null){
            flowOutEd.setText(bean.getFlow_out());
        }
        if (bean.getCourt_log()!=null){
            switch (bean.getCourt_log()){
                case "1":
                    logTv.setText("无");
                    logTv.setTextColor(getResources().getColor(R.color.blue));
                    logBean=new TextItemBean();
                    logBean.setId("1");
                    logBean.setName("无");
                    break;
                case "2":
                    logTv.setText("有");
                    logTv.setTextColor(getResources().getColor(R.color.blue));
                    logBean=new TextItemBean();
                    logBean.setId("2");
                    logBean.setName("有");
                    break;

            }
        }
        if (bean.getLicense_address()!=null){
            licenseAddEd.setText(bean.getLicense_address());
        }
        if (bean.getOffice_address()!=null){
            officeAddEd.setText(bean.getOffice_address());
        }
    }
    private void initTypeDialog(){
        mTypeDialog=new TextChoseDialog(this);
        mTypeDialog.setOnTextChoseListener(new TextChoseDialog.OnTextChoseListener() {
            @Override
            public void onChose(int position, TextItemBean item) {
                typeBean=item;
                typeTv.setText(typeBean.getName());
                typeTv.setTextColor(getResources().getColor(R.color.blue));
            }

        });
        List<TextItemBean> itemBeanList=new ArrayList<>();
        TextItemBean itemBean=new TextItemBean();
        itemBean.setId("1");
        itemBean.setName("未知");
        itemBeanList.add(itemBean);
        itemBean=new TextItemBean();
        itemBean.setId("2");
        itemBean.setName("国企");
        itemBeanList.add(itemBean);
        itemBean=new TextItemBean();
        itemBean.setId("3");
        itemBean.setName("私企");
        itemBeanList.add(itemBean);
        itemBean=new TextItemBean();
        itemBean.setId("4");
        itemBean.setName("个体");
        itemBeanList.add(itemBean);
        mTypeDialog.setData(itemBeanList);
    }
    private void initLogDialog(){
        mLogDialog=new TextChoseDialog(this);
        mLogDialog.setOnTextChoseListener(new TextChoseDialog.OnTextChoseListener() {
            @Override
            public void onChose(int position, TextItemBean item) {
                logBean=item;
                logTv.setText(logBean.getName());
                logTv.setTextColor(getResources().getColor(R.color.blue));
            }

        });
        List<TextItemBean> itemBeanList=new ArrayList<>();
        TextItemBean itemBean=new TextItemBean();
        itemBean.setId("1");
        itemBean.setName("无");
        itemBeanList.add(itemBean);
        itemBean=new TextItemBean();
        itemBean.setId("2");
        itemBean.setName("有");
        itemBeanList.add(itemBean);
        mLogDialog.setData(itemBeanList);
    }
    @Override
    public void initData() {
        getCompanyInfo();
    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.COMPANY_INFO)){
            if (responseInfo.isRequestOk){
                try{
                    UserCompanyDataBean baseBean= JSON.parseObject(responseInfo.content,UserCompanyDataBean.class);
                    bean=baseBean.getCompany_info();
                    setData();
                    loadingView.finish();
                }catch ( Exception e){
                    loadingView.fail();
                }

            }else {
                loadingView.fail();
            }
        }else if (responseInfo.url.equals(Contant.COMPANY_EDIT)){
            if (responseInfo.isRequestOk){
                Log.e("zwj",responseInfo.content);
                finish();
            }
        }
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.confirm:
                setCompanyInfo();
                break;
            case R.id.type_lay:
                if (mTypeDialog==null)
                    initTypeDialog();
                mTypeDialog.show();
                break;
            case R.id.log_lay:
                if (mLogDialog==null)
                    initLogDialog();
                mLogDialog.show();
                break;
            case R.id.reg_time_lay:
                TimePickerView pvTime;
                pvTime = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
                //控制时间范围
                Calendar calendar = Calendar.getInstance();
                pvTime.setRange(1900, calendar.get(Calendar.YEAR));//要在setTime 之前才有效果

                if (regTimeTv.getText().equals("请选择")) {
                    pvTime.setTime(new Date());
                } else {
                    pvTime.setTime(CommonUtil.Date2Millis(regTimeTv.getText().toString()));
                }
                pvTime.setCyclic(true);
                pvTime.setCancelable(true);
                //时间选择后回调
                pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

                    @Override
                    public void onTimeSelect(Date date) {
                        regTimeTv.setText(CommonUtil.DateFormat(date));
                        regTimeTv.setTextColor(getResources().getColor(R.color.blue));
                    }
                });
                pvTime.show();
                break;
        }
    }
}
